//
//  ActiveVideoViewController.m
//  YXFinance
//
//  Created by uplooking on 2018/10/23.
//  Copyright © 2018年 CRLong. All rights reserved.
//

#import "ActiveVideoViewController.h"

@interface ActiveVideoViewController ()
<
UITableViewDataSource,
UITableViewDelegate,
DZNEmptyDataSetDelegate,
DZNEmptyDataSetSource
>
{
    UITableView *tableView;
    NSMutableArray *arrayResult;
    BOOL isLoading;
    BOOL isCanLoadMore;
    ListLoadingFooterView *vListLoading;
    UIView *vListFooterNone;
    NSInteger page;
    NSInteger row;
}

@end

@implementation ActiveVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBackLeftBarButtonItemAction:@selector(backAction:)];
    [self initView];
    [self startSKV];
    [self getNewsListData];
}

-(void)backAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) initView{
    
    isLoading = YES;
    row=10;
    
    arrayResult=[NSMutableArray arrayWithCapacity:0];
    
    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, kScreenHeight - NAVIGATION_BAR_HEIGHT - 50 * kAutoScanleX - 49 * kAutoScanleX)];
    [tableView setBackgroundColor:[UIColor whiteColor]];
    [tableView setDelegate:self];
    [tableView setDataSource:self];
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [tableView setShowsHorizontalScrollIndicator:NO];
    [tableView setShowsVerticalScrollIndicator:NO];
    [tableView setEmptyDataSetDelegate:self];
    [tableView setEmptyDataSetSource:self];
    tableView.mj_header = [ZNDIYRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNewsListData)];
    [self.view addSubview:tableView];
    
    vListFooterNone = [UIView new];
    [tableView setTableFooterView:vListFooterNone];
    vListLoading = [ListLoadingFooterView initLoadingFooter];

}

#pragma mark--tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSInteger count=[arrayResult count];
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"SeckillGoodsCell";
    
    NewsIndexTableViewCell *cell=[_tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil)
    {
        cell=[[NewsIndexTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
     NewsIndexModel *model=arrayResult[indexPath.row];
    [cell setModel:model];
    
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 185 * kAutoScanleX;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
  
    NewsIndexModel * model = arrayResult[indexPath.row];
    
    InformationDetailsViewController * vc = [[InformationDetailsViewController alloc]init];
    vc.n_id = model.n_id;
    [[BaseTabBarViewController shareInstance].navigationController pushViewController:vc animated:YES];
    
    
}


#pragma mark--获取商品列表数据
-(void)getNewsListData{
    
    page=0;
    
    [self getNewsListPageData];
    
}

-(void)getNewsListPageData{
    
    page ++;
    isLoading = YES;
    isCanLoadMore = NO;
    
    WS(weakSelf);
    [HttpRequestHelper requestApiNewsType:1 page:page nums:row Block:^(NSString *msg, NSArray *array) {
        
        [tableView.mj_header endRefreshing];
        [weakSelf stopSKV];
        [weakSelf hideHud];
        isLoading = NO;
        
        if (page == 1 && [arrayResult count] != 0)
        {
            [arrayResult removeAllObjects];
            [tableView reloadData];
        }
        
        if (REQUEST_SUCCESS) {
            
            if ([array count]==row)
            {
                isCanLoadMore = YES;
                
                [tableView setTableFooterView:vListLoading];
            }else
            {
                [tableView setTableFooterView:vListFooterNone];
            }
            
            
            [arrayResult addObjectsFromArray:array];
            
            
        }else{
            
            [weakSelf showHint:msg];
        }
        
        [tableView reloadData];
        
        
    }];
    
}



- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView
{
    UIView *view = [[UIView alloc] init];
    
    
    if (isLoading)
    {
        
    }else
    {
        UIImageView *ivIcon = [[UIImageView alloc] init];
        [ivIcon setImage:[UIImage imageNamed:@"icon_none"]];
        [ivIcon setCenter:CGPointMake(self.view.current_w/2.0,-40)];
        [view addSubview:ivIcon];
        [ivIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view) ;
            make.centerX.equalTo(view);
            make.size.mas_equalTo(CGSizeMake(62, 62));
        }];
        
        UILabel *label = [[UILabel alloc] init];
        [label setTextColor:RGBACOLOR(204, 204, 204, 1)];
        [label setText:@"暂无数据"];
        [label setTextAlignment:NSTextAlignmentCenter];
        [label setFont:[UIFont systemFontOfSize:16]];
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(ivIcon.mas_bottom).offset(10);
            make.left.right.equalTo(view);
            make.height.mas_equalTo(20);
        }];
        
    }
    
    
    return view;
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    BOOL isReachBottom = tableView.contentOffset.y >= tableView.contentSize.height - tableView.bounds.size.height;
    
    if (isReachBottom && isCanLoadMore && !isLoading)
    {
        [self getNewsListPageData];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
